<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/8/24
 * Time: 17:45
 */

namespace Wechat\Controller;


use Common\Controller\AdminbaseController;
/*发布转介后台控制器*/
class ZhuanhouController extends AdminbaseController
{
    /*转介出租*/
    public function index()
    {
        $where = array('lt',99999999999);
        $count      = M('zhuanjie')->where(['type'=>1,'sort'=>$where])->order('id desc')->count();
        $Page       = new \Think\Page($count,10);
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();
        $list = M('zhuanjie')->where(['type'=>1,'sort'=>$where])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign(['page'=>$show,'data'=>$list,'empty'=>'<span class="empty">没有数据!</span>']);
        $this->display();
    }
    /*转介出售*/
    public function index1()
    {
        $where = array('lt',99999999999);
        $count      = M('zhuanjie')->where(['type'=>2,'sort'=>$where])->order('id desc')->count();
        $Page       = new \Think\Page($count,10);
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();
        $list = M('zhuanjie')->where(['type'=>2,'sort'=>$where])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign(['page'=>$show,'data'=>$list]);
        $this->display();
    }
    /*转介新房*/
    public function index2()
    {
        $where = array('lt',99999999999);
        $count      = M('zhuanjie')->where(['type'=>3,'sort'=>$where])->order('id desc')->count();
        $Page       = new \Think\Page($count,10);
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();
        $list = M('zhuanjie')->where(['type'=>3,'sort'=>$where])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign(['page'=>$show,'data'=>$list]);
        $this->display();
    }
    /*转介删除*/
    public function del()
    {
        $id = i('get.id');
        if(M('zhuanjie')->where(['id'=>$id])->delete()){
            $this->success('删除成功!~');
        }else{
            $this->error('失败，未知错误');
        }
    }
    /*添加阅读*/
    public function add2()
    {
        $id = i('get.id');
        $data = M('zhuanjie')->where(['id'=>$id])->find();
        $read = $data['read']+1;
        $return = ['read'=>$read,'status'=>1,'id'=>$id];
        if(M('zhuanjie')->where(['id'=>$id])->save(['read'=>$read])){
            echo json_encode($return);
        }else{
            echo 2;
        }
    }
    /*修改阅读量*/
    public function edit2()
    {
        $id = i('get.id');
        if(IS_POST){
            $post = i('post.');
            if(M('zhuanjie')->where(['id'=>$id])->save(['read'=>$post['sort']])){
                $this->success('修改成功!~',U('Zhuanhou/index'));
            }else{
                $this->error('失败，未知错误');
            }
        }else{
            $data = M('zhuanjie')->where(['id'=>$id])->find();
            $this->assign('vo',$data);
            $this->display();
        }
    }
    /*修改排序*/
    public function edit()
    {
        $id = i('get.id');
        if(IS_POST){
            $post = i('post.');
            if(M('zhuanjie')->where(['id'=>$id])->save(['sort'=>$post['sort']])){
                $this->success('修改成功!~',U('Zhuanhou/index'));
            }else{
                $this->error('失败，未知错误');
            }
        }else{
            $data = M('zhuanjie')->where(['id'=>$id])->find();
            $this->assign('vo',$data);
            $this->display();
        }
    }
    /*批量删除*/
    public function delall()
    {
        $post = i('post.');
        if(IS_POST) {
            foreach ($post['ids'] as $k) {
                M('zhuanjie')->where(['id' => $k])->delete();
            }
            $this->redirect(U('Zhuanhou/index'));
        }
    }
    /*搜索*/
    public function ser()
    {
        $where2 = array('lt',99999999999);
        $ser = i('post.ser');
        $where= array('like','%'.$ser.'%');
        if(IS_POST){
            if(trim(empty($ser))){
                $this->error('查询内容不能为空');
            }
            $count      = M('zhuanjie')->where(['phone|name'=>$where,'sort'=>$where2])->count();
            if($count==0){
                $this->error('没有找到你要找的内容,抱歉了。');
            }
            $Page       = new \Think\Page($count,10);
            $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
            $show       = $Page->show();
            $list = M('zhuanjie')->where(['phone|name'=>$where,'sort'=>$where2])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

            $this->assign(['page'=>$show,'data'=>$list]);
            $this->display();
        }else{
            $this->display();
        }
    }
}